﻿// http://blogs.dotnet-france.com/leonardl/post/RIA-Services-Un-mock-pour-faciliter-la-testabilite.aspx
namespace RialternativeSample.Tests.Client
{
    using System;
    using System.Collections.Generic;
    using System.ComponentModel.DataAnnotations;
    using System.Linq;
    using System.ServiceModel.DomainServices.Client;

    /// <summary>
    /// Provides a mock to the DomainClient class for unit testing.
    /// </summary>
    public abstract class MockDomainClient : DomainClient
    {
        /// <summary>
        /// Method called by the framework to begin the asynchronous query operation.
        /// </summary>
        /// <param name="query">The query to invoke.</param>
        /// <param name="callback">The callback to invoke when the query has been executed.</param>
        /// <param name="userState">Optional user state associated with this operation.</param>
        /// <returns>
        /// An asynchronous result that identifies this query.
        /// </returns>
        protected override IAsyncResult BeginQueryCore(EntityQuery query, AsyncCallback callback, object userState)
        {
            var result = new MockDomainClientAsyncResult()
            {
                Entities = this.MockQuery(query),
                AsyncState = userState,
                IsCompleted = true
            };

            if (callback != null)
            {
                callback(result);
            }

            return result;
        }

        /// <summary>
        /// Method called by the framework to complete the asynchronous query operation.
        /// </summary>
        /// <param name="asyncResult">An asynchronous result that identifies a query.</param>
        /// <returns>The results returned by the query.</returns>
        protected override QueryCompletedResult EndQueryCore(IAsyncResult asyncResult)
        {
            var result = asyncResult as MockDomainClientAsyncResult;

            var queryResult = new QueryResult<Entity>(result.Entities);

            return new QueryCompletedResult(
                            queryResult.GetRootResults().Cast<Entity>(),
                            queryResult.GetIncludedResults().Cast<Entity>(),
                            queryResult.TotalCount,
                            new ValidationResult[0]);
        }

        /// <summary>
        /// Mocks the specified query.
        /// </summary>
        /// <param name="query">The query.</param>
        /// <returns>The collection of entity returned by the query.</returns>
        protected abstract IEnumerable<Entity> MockQuery(EntityQuery query);

        /// <summary>
        /// Method called by the framework to complete an asynchronous invocation.
        /// </summary>
        /// <param name="asyncResult">An asynchronous result that identifies an invocation.</param>
        /// <returns>The results returned by the invocation.</returns>
        protected override InvokeCompletedResult EndInvokeCore(IAsyncResult asyncResult)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Method called by the framework to complete an asynchronous submit operation.
        /// </summary>
        /// <param name="asyncResult">An asynchronous result that identifies a submit request.</param>
        /// <returns>
        /// The results returned by the submit request.
        /// </returns>
        protected override SubmitCompletedResult EndSubmitCore(IAsyncResult asyncResult)
        {
            throw new NotImplementedException();
        }
    }
}
